<template>
  <div class="app-container">
    <!-- 数据表格 -->
    <el-table
      v-loading="listLoading"
      element-loading-text="数据加载中"
      :data="data"
      border
      fit
      highlight-current-row
    >
      <!-- 序号 -->
      <el-table-column prop="id" label="序号" width="70" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <!-- 项目名称 -->
      <el-table-column label="项目名称" width="200px" align="center">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_black" class="proName">{{
            scope.row.name
          }}</a>
        </template>
      </el-table-column>
      <!-- 项目描述 -->
      <el-table-column label="项目描述" align="center">
        <template slot-scope="scope">{{
          scope.row.description.toString()
        }}</template></el-table-column
      >
      <!-- 项目预览图 -->
      <el-table-column label="项目预览图" width="150px" align="center">
        <template slot-scope="scope">
          <!-- 图片 -->
          <el-image
            style="width: 100px"
            :src="scope.row.thumb"
            fit="contain"
            :preview-src-list="srcList"
          ></el-image>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="130px">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="GitHub"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              size="mini"
              type="info"
              icon="iconfont icon-github"
              circle
              @click="goToGitHub(scope.row)"
            ></el-button>
          </el-tooltip>

          <!-- 编辑 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editProjectHandle(scope.row)"
            ></el-button>
          </el-tooltip>
          <!-- 删除 -->
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteProject(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog
      title="请编辑该项目信息"
      :visible.sync="dialogVisible"
      fullscreen
    >
      <el-form :model="form">
        <el-form-item label="项目名称" label-width="120px">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="项目描述" label-width="120px">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="项目地址" label-width="120px">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="GitHub地址" label-width="120px">
          <el-input v-model="form.github"></el-input>
        </el-form-item>
        <el-form-item label="预览图" label-width="120px">
          <Upload v-model="form.thumb" />
        </el-form-item>
        <el-form-item label="排序等级" label-width="120px">
          <el-select
            v-model="form.order"
            label-width="120px"
            placeholder="分类等级"
          >
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEditProjectHandle">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getProjectList, delProject, updateProject } from "@/api/project";
import { server_URL } from "@/urlConfig.js";
import Upload from "@/components/Upload.vue";
export default {
  data() {
    return {
      data: [], //存储文章数据
      listLoading: false,
      srcList: [],
      dialogVisible: false,
      form: {
        name: "",
        description: "",
        url: "",
        github: "",
        thumb: "",
        order: 1,
      },
    };
  },
  components: {
    Upload,
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getProjectList().then(({ data }) => {
        this.listLoading = false;
        this.data = data;

        for (let i of this.data) {
          i.thumb = server_URL + i.thumb;
          this.srcList.push(i.thumb);
        }
      });
    },

    // 项目地址
    goToGitHub() {
      window.open(projectInfo.github);
    },
    // 编辑项目
    editProjectHandle(projectInfo) {
      this.dialogVisible = true;
      this.thumb = projectInfo.thumb;
      this.form = {
        ...projectInfo,
        description: projectInfo.description.toString(),
      };
      console.log(projectInfo);
    },
    // 删除项目
    deleteProject(projectInfo) {
      this.$confirm(
        "是否永久性删除项目，点击确认后项目数据不可恢复，请谨慎操作！！！",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          delProject(projectInfo.id).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 提交
    confirmEditProjectHandle() {
      let obj = { ...this.form };
      obj.description = obj.description.split(",");
      obj.order = parseInt(obj.order);
      obj.thumb = obj.thumb.replace(
        /^[a-z]{4,5}\:\/{2}[a-z]{1,}\:[0-9]{1,3}.(.*)/,
        "$1"
      );

      updateProject(obj.id, obj).then((res) => {
        this.dialogVisible = false;
        this.fetchData();
        this.$message({
          type: "success",
          message: "修改成功!",
        });
      });
    },
  },
};
</script>

<style>
.proName {
  cursor: pointer;
}
</style>